﻿using System;
using System.Windows.Forms;

namespace Microsoft.Office.PowerPoint.STAMP.Controls
{
    /// <summary>
    /// Toolbar containing controls for editing captions.
    /// </summary>
    public partial class CaptionEditingToolbar : UserControl
    {
        /// <summary>
        /// Occurs when the add caption button is clicked.
        /// </summary>
        public event EventHandler AddClicked;
        /// <summary>
        /// Occurs when the delete caption button is clicked.
        /// </summary>
        public event EventHandler DeleteClicked;
        /// <summary>
        /// Occurs when the sort captions button is clicked.
        /// </summary>
        public event EventHandler SortClicked;
        /// <summary>
        /// Occurs when the set start time button is clicked.
        /// </summary>
        public event EventHandler SetStartClicked;
        /// <summary>
        /// Occurs when the set end time button is clicked.
        /// </summary>
        public event EventHandler SetEndClicked;
        /// <summary>
        /// Occurs when the playback speed is changed.
        /// </summary>
        public event EventHandler<PlaybackSpeedChangedEventArgs> PlaybackSpeedChanged;

        /// <summary>
        /// Occurs when [fade duration changed].
        /// </summary>
        public event EventHandler<FadeDurationChangedEventArgs> FadeDurationChanged;

        /// <summary>
        /// Occurs when transparency is changed.
        /// </summary>
        public event EventHandler<TransparencyChangedEventArgs> TransparencyChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="CaptionEditingToolbar"/> class.
        /// </summary>
        public CaptionEditingToolbar()
        {
            InitializeComponent();
            playbackSpeedDropdown.SelectedIndex = (int) PlaybackSpeed.Normal;
        }

        /// <summary>
        /// Gets or sets the duration of the fade.
        /// </summary>
        public float FadeDuration
        {
            get { return durationTextbox.Value; }
            set { durationTextbox.Value = value; }
        }

        /// <summary>
        /// Gets or sets the transparency.
        /// </summary>
        public int Transparency
        {
            get { return transparencyTextbox.Value; }
            set { transparencyTextbox.Value = value; }
        }

        /// <summary>
        /// Increases the playback speed.
        /// </summary>
        public void IncreasePlaybackSpeed()
        {
            playbackSpeedDropdown.SelectedIndex = playbackSpeedDropdown.SelectedIndex == (int) PlaybackSpeed.Fast
                                                      ? (int) PlaybackSpeed.Faster
                                                      : (int) PlaybackSpeed.Fast;
        }

        /// <summary>
        /// Sets the playback speed to <see cref="PlaybackSpeed.Normal"/>
        /// </summary>
        public void ResetPlaybackSpeed()
        {
            playbackSpeedDropdown.SelectedIndex = (int) PlaybackSpeed.Normal;
        }

        /// <summary>
        /// Decreases the playback speed.
        /// </summary>
        public void DecreasePlaybackSpeed()
        {
            playbackSpeedDropdown.SelectedIndex = playbackSpeedDropdown.SelectedIndex == (int)PlaybackSpeed.Slow
                                                      ? (int)PlaybackSpeed.VerySlow
                                                      : (int)PlaybackSpeed.Slow;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            if (AddClicked != null)
            {
                AddClicked(this, EventArgs.Empty);
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (DeleteClicked != null)
            {
                DeleteClicked(this, EventArgs.Empty);
            }
        }

        private void sortButton_Click(object sender, EventArgs e)
        {
            if (SortClicked != null)
            {
                SortClicked(this, EventArgs.Empty);
            }
        }

        private void setStartButton_Click(object sender, EventArgs e)
        {
            if (SetStartClicked != null)
            {
                SetStartClicked(this, EventArgs.Empty);
            }
        }

        private void setEndButton_Click(object sender, EventArgs e)
        {
            if (SetEndClicked != null)
            {
                SetEndClicked(this, EventArgs.Empty);
            }
        }

        private void playbackSpeedDropdown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PlaybackSpeedChanged != null)
            {
                PlaybackSpeedChanged(this, new PlaybackSpeedChangedEventArgs((PlaybackSpeed) playbackSpeedDropdown.SelectedIndex));
            }
        }

        private void durationTextbox_TextChanged(object sender, EventArgs e)
        {
            if (FadeDurationChanged != null)
            {
                FadeDurationChanged(this, new FadeDurationChangedEventArgs(durationTextbox.Value));
            }
        }

        private void transparencyTextbox_TextChanged(object sender, EventArgs e)
        {
            if (TransparencyChanged != null)
            {
                TransparencyChanged(this, new TransparencyChangedEventArgs(transparencyTextbox.Value));
            }
        }
    }

    /// <summary>
    /// Contains the updaed fade duration
    /// </summary>
    public class FadeDurationChangedEventArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets the duration of the fade.
        /// </summary>
        /// <value>
        /// The duration of the fade.
        /// </value>
        public float FadeDuration { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FadeDurationChangedEventArgs"/> class.
        /// </summary>
        /// <param name="fadeDuration">Duration of the fade.</param>
        public FadeDurationChangedEventArgs(float fadeDuration)
        {
            FadeDuration = fadeDuration;
        }
    }

    /// <summary>
    /// Contains the updaed fade duration
    /// </summary>
    public class TransparencyChangedEventArgs : EventArgs
    {
        /// <summary>
        /// Gets or sets the duration of the fade.
        /// </summary>
        /// <value>
        /// The duration of the fade.
        /// </value>
        public int Transparency { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="FadeDurationChangedEventArgs"/> class.
        /// </summary>
        /// <param name="transparency">Duration of the fade.</param>
        public TransparencyChangedEventArgs(int transparency)
        {
            Transparency = transparency;
        }
    }

    /// <summary>
    /// Contains the updated playback speed when the playback speed is changed.
    /// </summary>
    public class PlaybackSpeedChangedEventArgs : EventArgs
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PlaybackSpeedChangedEventArgs"/> class.
        /// </summary>
        /// <param name="selectedPlaybackSpeed">The selected playback speed.</param>
        public PlaybackSpeedChangedEventArgs(PlaybackSpeed selectedPlaybackSpeed)
        {
            SelectedPlaybackSpeed = selectedPlaybackSpeed;
        }

        /// <summary>
        /// Gets the selected playback speed.
        /// </summary>
        public PlaybackSpeed SelectedPlaybackSpeed { get; private set; }
    }
}
